package com.itsv.cyzx.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.itsv.cyzx.bean.BusinessaddBean;

public class DBHelper_Application extends SQLiteOpenHelper {

	SQLiteDatabase db = null;
	private static final int dbVersion = 2;
	private Context context = null;
	private static String dbname = "businessapp.db";

	public DBHelper_Application(Context context) {
		super(context, dbname, null, dbVersion);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// 创建联系人表
		String tablesql = "create table Tab_Contacts(appName varchar(20),appTime varchar(20),appType varchar(20),appUnit varchar(20),endTime varchar(20),reason varchar(20),startTime varchar(20),"
				+ "appStatu varchar(20))";
		db.execSQL(tablesql);
		// 创建联系人版本表
//		tablesql = "create table Tab_ContactsBanBen(version varchar(20))";
//		db.execSQL(tablesql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (newVersion <= oldVersion) {
			return;
		} else {
			context.deleteDatabase(dbname);
			onCreate(db);
		}
	}

	/**
	 * 获取事务列表
	 * 
	 * @return
	 */
	public ArrayList<BusinessaddBean> getContactsList() {
		ArrayList<BusinessaddBean> list = new ArrayList<BusinessaddBean>();
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		Cursor cursor = null;
		String sql = "select * from Tab_Contacts";
		cursor = db.rawQuery(sql, null);
		BusinessaddBean bean = null;

		while (cursor.moveToNext()) {
			bean = new BusinessaddBean();
			bean.setAppName(cursor.getString(cursor.getColumnIndex("appName")));
			bean.setAppTime(cursor.getString(cursor.getColumnIndex("appTime")));
			bean.setAppType(cursor.getString(cursor.getColumnIndex("appType")));
			bean.setAppUnit(cursor.getString(cursor.getColumnIndex("appUnit")));
			bean.setEndTime(cursor.getString(cursor.getColumnIndex("endTime")));
			bean.setReason(cursor.getString(cursor
					.getColumnIndex("reason")));
			bean.setStartTime(cursor.getString(cursor.getColumnIndex("startTime")));
			bean.setAppStatu(cursor.getString(cursor.getColumnIndex("appStatu")));
			list.add(bean);
		}
		cursor.close();
		return list;
	}

	/**
	 * 添加联系人信息
	 * 
	 * @param contactsBean
	 */
	public void setContacts(BusinessaddBean contactsBean) {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		ContentValues values = new ContentValues();
		values.put("appName", contactsBean.getAppName());
		values.put("appTime", contactsBean.getAppTime());
		values.put("appType", contactsBean.getAppType());
		values.put("appUnit", contactsBean.getAppUnit());
		values.put("endTime", contactsBean.getEndTime());
		values.put("reason", contactsBean.getReason());
		values.put("startTime", contactsBean.getStartTime());
		values.put("appStatu", contactsBean.getAppStatu());
		db.insert("Tab_Contacts", null, values);
	}

	public void update(String appTime,String appStatu) {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		ContentValues values = new ContentValues();
		values.put("appStatu", appStatu);
		db.update("Tab_Contacts", values, "appTime=?", new String[] { appTime });
		db.close();
	}


}
